# -*- coding: utf-8 -*-
"""
@Version : 1.0.0
@Author  : ChenMZ
@Time    : 2024/01/22
@File    : decorator.py
@Desc    : 装饰器
@Modify  : 2024/01/22
"""
import threading


def singleton(cls):
    cls._instance = None
    cls._lock = threading.Lock()

    def _singleton(*args, **kwargs):
        with cls._lock:
            if not cls._instance:
                cls._instance = cls(*args, **kwargs)

        return cls._instance

    return _singleton


class Singleton(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = object.__new__(cls)
        return Singleton._instance


def overload_decorator(*signatures):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for sig_func in signatures:
                if all(isinstance(arg, sig_arg_type) for arg, sig_arg_type in zip(args, sig_func)):
                    return sig_func(*args, **kwargs)
            raise TypeError("No matching signature found.")
        return wrapper
    return decorator
